package com.yyds.controller;

import com.yyds.common.MyJSON;
import com.yyds.common.R;
import com.yyds.pojo.PersonResident;
import com.yyds.service.PersonResidentService;
import com.yyds.util.OssUtil;
import com.yyds.vo.EchartPieVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author 花君瑜
 * @date 2022/10/14 08:29:20
 * @desc 居民信息管理模块
 **/
@RestController
@RequestMapping("/admin/person/resident")
public class PersonResidentController {
    @Autowired
    private PersonResidentService personResidentService;

    //查询全部居民信息
    @RequestMapping("/queryAllResident")
    public MyJSON<PersonResident> queryAllPersonResidentController(@RequestParam Map<String, Object> params) {
        // 注意 这里使用 map来接受参数  前端传的不定参数 都可以封装到 map 中
        System.out.println("public MyJSON<PersonResident> personResidentList(@RequestParam Map<String, Object> params) ="+params);
        return personResidentService.queryAllPersonResidentService(params);
    }

    //根据id删除居民信息
    @PostMapping("/deleteResident")
    public R deletePersonResidentController(Integer resident_id) {
        System.out.println("public R deleteResidentById(Integer resident_id) = "+resident_id);
        int i = personResidentService.deletePersonResidentService(resident_id);
        return i > 0 ? R.ok() : R.error();
    }

    //删除部分居民信息
    @RequestMapping("/deleteAllResident")
    public R deleteAllPersonResidentController(String str) {
        System.out.println("deleteAllPersonResidentController str" + str);
        int i = personResidentService.deleteAllPersonResidentService(str);
        return i > 0 ? R.ok() : R.error();
    }

    //添加学生
    @RequestMapping("/addResident")
    public R addPersonResidentController(PersonResident personResident,String status_status){
        System.out.println("public R addPersonResidentController(PersonResident personResident,String status_status) = "+status_status);
        System.out.println("public R addPersonResidentController(PersonResident personResident) = "+personResident.getResident_name());
        System.out.println("public R addPersonResidentController(PersonResident personResident) = "+personResident.getResident_birthday());
        int result = personResidentService.addPersonResidentService(personResident,status_status);
        return result != 0 ? R.ok() : R.error();
    }

    //修改学生信息
    @RequestMapping("/updateResident")
    public R updatePersonResidentController(PersonResident personResident){
        System.out.println(personResident);
        int result = personResidentService.updatePersonResidentService(personResident);
        return result > 0 ? R.ok() : R.error();
    }

    //照片文件上传
    @PostMapping("/uploadPicture")
    public R uploadPicPersonResidentController(MultipartFile file) throws IOException {
        //得到文件的名字
        String originalFilename = file.getOriginalFilename();

        String fileUrl = OssUtil.uploadFile(file.getInputStream(), originalFilename);
        // 上传成功之后的文件地址 写给前端
        System.out.println(R.ok().data("url",fileUrl));
        return R.ok().data("url",fileUrl);
    }

    //查询男女 保密人数
    @RequestMapping("/queryPersonResidentEchartPie")
    public R queryPersonResidentController(){
        List<EchartPieVo> list = personResidentService.queryPersonResidentService();
        return R.ok().data("pie", list);
    }
}
